package com.wteam.dragon.model.controller;

import com.wteam.dragon.model.pojo.bean.Menu;
import com.wteam.dragon.model.pojo.dto.MenuDto;
import com.wteam.dragon.model.pojo.dto.RoleSmallDto;
import com.wteam.dragon.model.pojo.vo.MenuVo;
import com.wteam.dragon.model.pojo.vo.RolesMenusVo;
import com.wteam.dragon.model.service.MenuService;
import com.wteam.dragon.model.service.RoleService;
import com.wteam.dragon.model.service.RolesMenusService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.BeanUtil;
import com.wteam.dragon.system.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2020/12/18 10:57
 */
@Api(tags = "系统：菜单管理")
@RestController()
@RequestMapping("admin/menu")
@RequiredArgsConstructor
public class MenuController {
    private final MenuService menuService;
    private final RoleService roleService;
    private final RolesMenusService rolesMenusService;

    @Log("获得菜单树")
    @ApiOperation(value = "获得菜单树", notes = "测试")
    @GetMapping("treeMenu")
    @PreAuthorize("@el.check('treeMenu')")
    public ResultMessage treeMenu() {
        Long userId = UserUtil.getCurrentUserId();
        List<RoleSmallDto> roles = roleService.getRolesByUserId(userId);
        List<MenuDto> menuDtoList = menuService.getMenusByRoles(roles);
        return menuService.treeMenu(menuDtoList);
    }

    @Log("添加菜单")
    @ApiOperation(value = "添加菜单")
    @PostMapping("addMenu")
    @PreAuthorize("@el.check('addMenu')")
    public ResultMessage addMenu(MenuVo menuVo) {
        Menu menu = BeanUtil.from(menuVo, Menu.class);
        return menuService.addMenu(menu);
    }

    @Log("删除菜单")
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("delMenu")
    @PreAuthorize("@el.check('delMenu')")
    public ResultMessage delMenu(Long menuId) {
        return menuService.delMenu(menuId);
    }

    @Log("更新菜单")
    @ApiOperation(value = "更新菜单")
    @PutMapping("editMenu")
    @PreAuthorize("@el.check('editMenu')")
    public ResultMessage editMenu(MenuVo menuVo) {
        Menu menu = BeanUtil.from(menuVo, Menu.class);
        return menuService.editMenu(menu);
    }

    @Log("获得所有的菜单")
    @ApiOperation(value = "获得所有的菜单")
    @GetMapping("treeAllMenu")
    @PreAuthorize("@el.check('treeAllMenu')")
    public ResultMessage treeAllMenu(String title) {
        return menuService.treeAllMenu(title);
    }

    @Log("分配菜单给角色")
    @ApiOperation(value = "分配菜单给角色")
    @PostMapping("editRolesMenus")
    @PreAuthorize("@el.check('editRolesMenus')")
    public ResultMessage editRolesMenus(@RequestBody RolesMenusVo rolesMenusVo) {
        return rolesMenusService.editRolesMenus(rolesMenusVo.getRoleId(), rolesMenusVo.getMenuIds());
    }
}
